package com.artPreter.utils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringTool {

    public static HashMap ShortTimestampToDateString(){
        // 获取当前日期时间
        LocalDateTime now = LocalDateTime.now();
        // 获取当前时区
        ZoneId zone = ZoneId.systemDefault();
        // 获取本日零时的日期时间
        LocalDateTime startOfDay = now.withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endOfDay = now.withHour(23).withMinute(59).withSecond(59).withNano(9999);
        // 转换为具体时区的日期时间
        ZonedDateTime zonedDateTime = startOfDay.atZone(zone);
        ZonedDateTime zonedDateTime2 = endOfDay.atZone(zone);
        // 获取零时的时间戳
        long startTimestamp = zonedDateTime.toEpochSecond();
        long endTimestamp = zonedDateTime2.toEpochSecond();
        HashMap hashMap = new HashMap();
        hashMap.put("start",startTimestamp);
        hashMap.put("end",endTimestamp);
        return hashMap;
    }


    public static String ShortTimestampToDateString(Long shortTimestamp){
        String result = "";
        try {
            Long timestamp = shortTimestamp * 1000;
            Date date = new Date(timestamp); // 将时间戳转换为Date对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 指定日期格式
            result = sdf.format(date); // 格式化Date对象为字符串
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    public static String ShortTimestampToDateString2(Long shortTimestamp){
        String result = "";
        try {
            Long timestamp = shortTimestamp * 1000;
            Date date = new Date(timestamp); // 将时间戳转换为Date对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm"); // 指定日期格式
            result = sdf.format(date); // 格式化Date对象为字符串
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    public static String ShortTimestampToDateString3(Long shortTimestamp){
        String result = "";
        try {
            Long timestamp = shortTimestamp * 1000;
            Date date = new Date(timestamp); // 将时间戳转换为Date对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // 指定日期格式
            result = sdf.format(date); // 格式化Date对象为字符串
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    public static String StringGetNumber(String str){
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        String result = m.replaceAll("").trim();
        return result;
    }

    public String StringGetNumbers(String str){
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        String result = m.replaceAll("").trim();
        return result;
    }

    public static Integer AhToSortId(String ah){
        String ahY = ah.substring(0, ah.indexOf("2072"));
        String ahNo = ah.substring(12, ah.indexOf("号"));
        String year = StringGetNumber(ahY);
        String num = StringGetNumber(ahNo);
        Integer result = Integer.parseInt(year)*1000000 + Integer.parseInt(num);
        return result;
    }

    public static Float IntegerToFloat(Integer num){
        Integer str = new Integer(num);
        int i = str.intValue();
        float rr = (float)i;
        return  rr;
    }

    /**
     * 字符串转localdatetime
     * @param date
     * @return
     */
    public static LocalDateTime StringToDateTime(String date){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(date+" 00:00:00",df);
        return  ldt;
    }

    public static LocalDateTime StringToDateTime2(String date){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(date+" 00:00:00",df);
        return  ldt;
    }

    /**
     * yyyyMMdd HH:mm:ss字符串转10位时间戳
     * @param date
     * @return
     */
    public static long StringToTimestamp(String date){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(date+" 00:00:00",df);
        return ldt.toInstant(ZoneOffset.ofHours(8)).toEpochMilli()/1000;
    }

    /**
     * 本年第一天
     * @return
     */
    public static String getYearStart(){
        return new SimpleDateFormat("yyyy").format(new Date())+"-01-01 00:00:00";
    }

    /**
     * 获取本年的最后一天
     * @return String
     * **/
    public static String getYearEnd(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date currYearLast = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast)+" 23:59:59";
    }

    /**
     * 字符串转localdatetime
     * @param day
     * @return
     */
    public static LocalDateTime StringToLocalDateTime(String day){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(day,df);
        return ldt;
    }

    /**
     * 字符串转localdatetime
     * @param day
     * @return
     */
    public static LocalDateTime StringToLocalDateTime2(String day){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy.M.d HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(day,df);
        return ldt;
    }

    /**
     * 字符串转localdatetime
     * @param day
     * @return
     */
    public static LocalDateTime StringToLocalDateTime3(String day){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(day,df);
        return ldt;
    }

    public static String CalculationTimePeriod(LocalDateTime stime,LocalDateTime etime){
        String result = "";
        //获取秒数
        long startSecond = stime.toEpochSecond(ZoneOffset.ofHours(0));
        long endSecond = etime.toEpochSecond(ZoneOffset.ofHours(0));
        long absSeconds = Math.abs(startSecond - endSecond);

        //获取秒数
        long s = absSeconds % 60;
        //获取分钟数
        long m = absSeconds / 60 % 60;
        //获取小时数
        long h = absSeconds / 60 / 60 % 24;
        //获取天数
        long d = absSeconds / 60 / 60 / 24;

        result = d + "天" + h + "时" + m + "分" + s + "秒";

        return result;
    }

    /**
     * 字符串转localdatetime
     * @param day
     * @return
     */
    public static String LocalDateTimeToString(LocalDateTime day, Integer type){
        String result = "";
        DateTimeFormatter df = DateTimeFormatter.ofPattern("HH:mm");;
        switch (type){
            case 1:
                df = DateTimeFormatter.ofPattern("HH:mm");
                break;
            case 2:
                df = DateTimeFormatter.ofPattern("YYYY-MM-dd");
                break;
            case 3:
                df = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm");
                break;
        }
        result = df.format(day);
        return result;

    }




}
